package com.lhb.test.print;

import javax.print.attribute.Size2DSyntax;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.OrientationRequested;

public class PrintUtil {
	
	public final static MediaSizeName[] mediaSizeNameAry = new MyMediaSizeName().getMediaSizeNameAry();
	
	/**
	 * 纸张尺寸
	 * @param value
	 * @return
	 */
	public static MediaSizeName getMediaSizeName(int value){
		if(value<0||value>=mediaSizeNameAry.length){
			throw new RuntimeException("非法的纸张尺寸："+value);
		}
		return mediaSizeNameAry[value];
	}
	
	/**
	 * // 0:纵向，1：横向 
	 * @param value
	 * @return
	 */
	public static OrientationRequested getOrientationRequested(int value){
		if(value==0){
			return OrientationRequested.PORTRAIT;//纵向打印
		}
		return OrientationRequested.LANDSCAPE;//横向打印
	}
	
	
	public static MediaPrintableArea getReportMediaPrintableArea(int paperSize,
			float marginTop,float marginBottom,float marginLeft,float marginRight){
		MediaSizeName mediaSizeName = getMediaSizeName(paperSize);
		MediaSize mediaSize = MediaSize.getMediaSizeForName(mediaSizeName);
		float width = mediaSize.getX(Size2DSyntax.MM);
		float height = mediaSize.getY(Size2DSyntax.MM);
		
		float x = marginLeft;
		float y = marginTop;
		float w = width-x-marginRight;
		float h = height-y-marginBottom;
		System.out.println("marginTop:"+marginTop+",marginBottom:"+marginBottom+",marginLeft:"+marginLeft+",marginRight:"+marginRight);
		System.out.println("width:"+width+",height:"+height+",x:"+x+",y:"+y+",w:"+",h:"+h);
		MediaPrintableArea mediaPrintableArea = new MediaPrintableArea(x, y, w, h, Size2DSyntax.MM);
		return mediaPrintableArea;
	}
	
	
	/**
	 * 打印区域
	 * @param printScheme
	 * @return
	 */
	public static MediaPrintableArea getMediaPrintableArea(int paperSize,int orientation,
			float marginTop,float marginBottom,float marginLeft,float marginRight){
		MediaSizeName mediaSizeName = getMediaSizeName(paperSize);
		MediaSize mediaSize = MediaSize.getMediaSizeForName(mediaSizeName);
		float width = mediaSize.getX(Size2DSyntax.MM);
		float height = mediaSize.getY(Size2DSyntax.MM);
		if(orientation==1){//打印方向 0:纵向，1：横向  
			float temp = width;
			width = height;
			height = temp;
		};
		
		return getMediaPrintableArea(width, height, marginTop, marginBottom, marginLeft, marginRight);
	}
	
	/**
	 * 打印区域
	 * @param width
	 * @param height
	 * @param marginTop
	 * @param marginBottom
	 * @param marginLeft
	 * @param marginRight
	 * @return
	 */
	public static MediaPrintableArea getMediaPrintableArea(float width,float height,
			float marginTop,float marginBottom,float marginLeft,float marginRight){
		System.out.println("width:"+width+",height:"+height);
		float unit=1f;
		float x = marginLeft*unit;
		float y = marginTop*unit;
		float w = width-x-marginRight*unit;
		float h = height-y-marginBottom*unit;
		MediaPrintableArea mediaPrintableArea = new MediaPrintableArea(x, y, w, h, Size2DSyntax.MM);
		return mediaPrintableArea;
	}
	
	private static class MyMediaSizeName extends MediaSizeName{
		
		private static final long serialVersionUID = -5137978268957858717L;

		public MyMediaSizeName() {
			super(0);
		}
		
		public MediaSizeName[] getMediaSizeNameAry(){
			return (MediaSizeName[])this.getEnumValueTable();
		}
		
	}
}
